# encoding = utf-8
class BuildingsController < ApplicationController

  before_filter :authenticate_admin!, :only => [:new, :create, :list, :edit, :update, :delete]

  def new
    @building = Building.new
    @action = {:action => 'create'}
    @action_title = '添加新办公楼'
    render :layout => 'admins'
  end

  def create
    building = Building.new(params[:building])

    if building.save
      flash[:success] = '添加办公楼成功'
    else
      flash[:error] = '添加办公楼失败'
    end

    redirect_to :action => "list"
  end

  def list
    @buildings = Building.order('created_at desc').paginate :page => params[:page], :per_page => PAGE_SIZE
    render :layout => 'admins'
  end

  def edit
    @building = Building.find(params[:id])
    @action = {:action => 'update', :id => @building.id}
    @action_title = '编辑办公楼'
    render :layout => 'admins', :action => 'new'
  end

  def update
    building = Building.update(params[:id], params[:building])
    if building
      flash[:success] = '办公楼修改成功'
    end
    redirect_to :action => "list"
  end

  def delete
    building = Building.find(params[:id])
    if building.destroy
      flash[:success] = '办公楼删除成功'
    end
    redirect_to :action => "list"
  end

  def companies
    @companies = Building.find(params[:id]).companies
    render :layout => false
  end
end
